<template>
  <el-dialog
    v-model="dialogFormVisible"
    :close-on-click-modal="false"
    :title="title"
    width="520px"
    @close="close"
  >
    <el-form
      v-if="editValue"
      ref="formRef"
      label-width="100px"
      :model="formDel"
      :rules="rules"
    >
      <el-form-item label="部门：" prop="id">
        <el-tree-select
          v-model="formDel.id"
          check-on-click-node
          check-strictly
          :data="list"
          :default-expanded-keys="[1]"
          node-key="id"
          :render-after-expand="false"
          show-checkbox
        >
          <template #default="{ data }">
            <span>{{ data.department_name }}</span>
          </template>
        </el-tree-select>
      </el-form-item>
    </el-form>
    <el-form
      v-else
      ref="formRef"
      label-width="100px"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="部门名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item prop="parent_id">
        <template #label>
          上级部门
          <el-tooltip content="0默认为一级" effect="dark" placement="top-start">
            <vab-icon icon="question-fill" />
          </el-tooltip>
          ：
        </template>
        <el-tree-select
          v-model="form.parent_id"
          check-on-click-node
          check-strictly
          :data="list"
          :default-expanded-keys="[1]"
          node-key="id"
          :render-after-expand="false"
          show-checkbox
        >
          <template #default="{ data }">
            <span>{{ data.department_name }}</span>
          </template>
        </el-tree-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit, departmentDelete } from '@/api/enterpriseWeChat/department'
  export default defineComponent({
    name: 'TableEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        formRef: null,
        treeRef: null,
        form: {
          id: null,
          name: '',
          parent_id: 0,
        },
        formDel: {
          id: null,
        },
        rules: {
          name: [
            { required: true, trigger: 'blur', message: '请输入部门名称' },
          ],
        },
        title: '',
        ids: [],
        dialogFormVisible: false,
        list: [],
        corp_id: '',
        editValue: '',
      })

      const showEdit = (corp_id, treeList, row) => {
        state.corp_id = corp_id
        state.list = treeList
        console.log(row)
        if (!row) {
          state.title = '创建部门'
          state.parent_id = 0
          state.editValue = ''
        } else {
          state.title = '删除部门'
          state.editValue = row
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        if (state.editValue) {
          state.formDel = {}
        } else {
          state.form = {}
          state.form.parent_id = 0
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            console.log(state.form)
            console.log(state.formDel)
            if (state.editValue) {
              const { message } = await departmentDelete({
                corp_id: state.corp_id,
                id: state.formDel.id,
              })
              $baseMessage(message, 'success', 'vab-hey-message-success')
            } else {
              const { message } = await doEdit({
                corp_id: state.corp_id,
                name: state.form.name,
                parent_id: state.form.parent_id,
              })
              $baseMessage(message, 'success', 'vab-hey-message-success')
            }
            emit('fetch-data')
            close()
          }
        })
      }
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>

<style lang="scss" scoped></style>
